package cn.com.thingo.shyintelligent.ui.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RoomInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.app.App;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.AreaSelectContract;
import cn.com.thingo.shyintelligent.presenter.AreaSelectPresenter;
import cn.com.thingo.shyintelligent.ui.adapter.RoomAdapter;
import cn.com.thingo.shyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.shyintelligent.util.ToastUtil;

public class AreaSelectActivity extends BaseActivity<AreaSelectPresenter> implements AreaSelectContract.View, RoomAdapter.OnItemClickListener {
    @BindView(R.id.area_select_list)
    RecyclerView rvList;
    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    private List<RoomInfo> roomList = new ArrayList<>();
    private RoomAdapter adapter;

    @Override
    protected void initEventAndData() {
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        List<RoomInfo> roomList = THOpenSDK.getInstance().getRoomList();
        if (roomList != null && roomList.size() > 0) {
            this.roomList.clear();
            this.roomList.addAll(roomList);
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        ivBack.setImageResource(R.mipmap.back_blue);
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.area_select);
        adapter = new RoomAdapter(roomList, mContext);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        adapter.setListener(this);
        rvList.setAdapter(adapter);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_area_select;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onItemClick(int position) {
        RoomInfo roomInfo = roomList.get(position);
        App.getInstance().setCurrentRoomInfo(roomInfo);
        finish();

    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }
}
